package com.itany.springcloud.springcloudnacosgateway6003.filter;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.itany.springcloud.springcloudnacosgateway6003.util.JWTUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AccessFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("AccessFilter.filter()");
        //获取request请求对象,或response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //获取URI路径
        String path = request.getURI().getPath();
        System.out.println("-------------------");
        System.out.println(path);
        System.out.println(request.getMethod());
        System.out.println("-------------------");
        //判断路径中如果含有/user/modify/** /user/remove/**,则需要鉴权
//        AntPathMatcher antPathMatcher = new AntPathMatcher();
//        if(antPathMatcher.match("/user/modify/**", path)||antPathMatcher.match("/user/remove/**", path)){
        //判断PUT DELETE,则需要鉴权
        if(request.getMethod().matches("PUT")||request.getMethod().matches("DELETE")){
            // 获取token
            HttpCookie tokenCookie=request.getCookies().getFirst("token");
            System.out.println(tokenCookie);
            if(tokenCookie==null){
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }


            String token=tokenCookie.getValue();
            DecodedJWT decodedJWT;
            try {
                decodedJWT=JWTUtil.decodedJWT(token);
            } catch (Exception e) {
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();

            }


            String username=decodedJWT.getClaim("username").asString();
            String password=decodedJWT.getClaim("password").asString();

            System.out.println("+++++++++++++++++++");
            System.out.println(username);
            System.out.println(password);
            System.out.println("+++++++++++++++++++");

            if(!"abc".equals(username)||!"123".equals(password)){
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }


            Map<String,String> payload=new HashMap<String,String>();
            payload.put("username",username);
            payload.put("password",username);
            String jwt= JWTUtil.getJWT(payload);

            response.addCookie(ResponseCookie.from("token", jwt).build());


        }

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
